package com.wx.service.impl;

import com.github.pagehelper.PageHelper;
import com.wx.mapper.CollectMapperCustom;
import com.wx.mapper.UserProductDetailMapperCustom;
import com.wx.mapper.WxCollectMapper;
import com.wx.pojo.WxCollect;
import com.wx.pojo.WxUserProductDetail;
import com.wx.service.ICollectService;
import com.wx.service.IOrderService;
import com.wx.service.IUserProductDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.List;

@Service
public class CollectServiceImpl implements ICollectService {


    @Autowired
    private WxCollectMapper wxCollectMapper;
    @Autowired
    private CollectMapperCustom collectMapperCustom;
    @Autowired
    private UserProductDetailMapperCustom userProductDetailMapperCustom;
    @Autowired
    private IUserProductDetailService iUserProductDetailService;
    @Autowired
    private IOrderService iOrderService;


    @Override
    public List<WxCollect> listCollect(){
        return wxCollectMapper.selectAll();
    }

    @Override
    public List<WxCollect> listCollect(WxCollect collect, Integer page,Integer pageSize){
        PageHelper.startPage(page,pageSize);
        Example example=new Example(WxCollect.class);
        Example.Criteria criteria=example.createCriteria();
        example.orderBy("id").desc();
        List<WxCollect> listCollect=wxCollectMapper.selectByExample(example);
        return listCollect;
    }

    @Override
    public WxCollect getModelByNumber(String number){
        return collectMapperCustom.getModelByNumber(number);
    }

    @Override
    public List<WxCollect> listModelByStatus(Integer status){
        return collectMapperCustom.listModelByStatus(status);
    }

    @Override
    public Integer updateModelAmount(String number,Integer amount){
        return collectMapperCustom.updateModelAmount(number,amount);
    }

    @Override
    public Integer updateModelDepot(String number,
                                    Integer depot_id,
                                    Integer logistics){
        return collectMapperCustom.updateModelDepot(number,depot_id,logistics);
    }

    @Override
    @Transactional
    public boolean joinCollect(String item, String number,Integer user_id,Integer depot_id,Integer logistics){

        if (!item.equals(" ")){
//            String number="";
            String[] arr1=item.split(";");
            for (String str1: arr1) {
                String[] arr2=str1.split(",");

//               修改我添加的产品品明细的吨数
                WxUserProductDetail model =new WxUserProductDetail();
                model.setUserId(user_id);
                model.setCollectNumber(number);
                model.setProductId(new Integer(arr2[0]));
                model.setAmount(new Integer(arr2[1]));
                iUserProductDetailService.updateModelAmount(model);

            }

            Integer amount=userProductDetailMapperCustom.countAmountByNumber(number);

            Object total_price=userProductDetailMapperCustom.countPriceByNumber(number);

            //修改集采总吨数
            collectMapperCustom.updateModelAmount(number,amount);

//            修改集采油库和提油方式
            collectMapperCustom.updateModelDepot(number,depot_id,logistics);

            //下订单
            iOrderService.addOrderID(user_id,number,amount,new BigDecimal(total_price.toString()));



        }else {
            return false;
        }
        return true;
    }

}
